package com.google.cloudsqldemo;
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import java.util.Date;

@SuppressWarnings("serial")
public class GuestbookServlet extends HttpServlet {
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	  throws IOException {
		String title = req.getParameter("title");
		String body = req.getParameter("body");      
		Post post = new Post();
		post.setTitle(title);
		post.setBody(body);
		post.setTimestamp(new Date());
		
		HibernateConfigSingleton hibernateConfig = HibernateConfigSingleton.getInstance();
		Session session = hibernateConfig.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(post);
		session.getTransaction().commit();
		session.close();
		
		resp.setHeader("Refresh","0; url=/guestbook.jsp");
	}  
}
